package com.liu;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PhoneServics {
	private Context context;
	private DataBaseOpenHelper dbHelper;

	public PhoneServics(Context context) {
		dbHelper = new DataBaseOpenHelper(context);

		this.context = context;
	}

	public void save(Phone phone) {
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		dbBase.execSQL("insert into callphone(num,count,cdata) values(?,?,?) ",new Object[]{phone.getNum(),phone.getCount(),phone.getCdata()});
		

	}

	public void update(String num) {
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		dbBase.execSQL("update callphone set count=count+1 where num=? ",new Object[]{num});
		
	}

	public void delete(String... nums) {
		if(nums.length>0){
		StringBuffer sb=new StringBuffer();
		for(String num:nums)
		{
			sb.append('?').append(',');
			
		}
		sb.deleteCharAt(sb.length()-1);
		
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		dbBase.execSQL("delete from callphone where num in (?,?,?) ",(Object[])nums);
		
		
		}

	}

	public Phone find(String num) {
		
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		Cursor cursor=dbBase.rawQuery("select * from callphone where num=?", new String[]{num});
       if(cursor.moveToNext()){
    	   return new Phone(cursor.getString(0),cursor.getInt(1),cursor.getString(2));
    	   
    	   
       }
    	   
		return null;

	}
	public List<Phone>  getdata ()
	{
		List<Phone> phones=new ArrayList<Phone>();
		
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		Cursor cursor=dbBase.rawQuery("select * from callphone ",null);// new String[]{String.valueOf(start),String.valueOf(end)});
		while(cursor.moveToNext()){
		phones.add(new Phone(cursor.getString(0),cursor.getInt(1),cursor.getString(2)));
		
			
		}
		
		return phones;
		
	}
	public List<Phone>  getdata (String num)
	{
		List<Phone> phones=new ArrayList<Phone>();
		
		SQLiteDatabase	dbBase= dbHelper.getWritableDatabase();
		Cursor cursor=dbBase.rawQuery("select * from callphone where num=?",new String[]{num} );// new String[]{String.valueOf(start),String.valueOf(end)});
		while(cursor.moveToNext()){
		phones.add(new Phone(cursor.getString(0),cursor.getInt(1),cursor.getString(2)));
		
			
		}
		
		return phones;
		
	}

	
}
